﻿using System;
using System.Collections.Generic;
using System.Text;
using lingopen.Application.CRM.Models.KF;
using lingopen.Application.WeiXin;
using Chloe;
using Newtonsoft.Json.Linq;

namespace lingopen.Application.CRM.Implements
{
    public class HelpersAppService : AdminAppService, IHelpersAppService
    {
        public void Add(CRM_KF_WXUser input)
        {
            var str = WXAPI.CreateKFAccount(this.DbContext, input.NickName, input.KFAccount, (new Random()).Next(100000, 1000000) + "");
            if (!String.IsNullOrEmpty(str))
            {
                //{
                //    "errcode" : 0,
                //    "errmsg" : "ok",
                //}
                var jo = JObject.Parse(str).ToObject<dynamic>();
                if (jo.errcode == 0)
                {
                    this.DbContext.Insert(input);
                }
                else
                {
                    throw new Exception((string)jo.errmsg);
                }
            }
            else
            {
                throw new Exception("网络错误！");
            }
        }

        public void Update(CRM_KF_WXUser input)
        {
            var str = WXAPI.UpdateKFAccount(this.DbContext, input.NickName, input.KFAccount, (new Random()).Next(100000, 1000000) + "");

            if (!String.IsNullOrEmpty(str))
            {
                //{
                //    "errcode" : 0,
                //    "errmsg" : "ok",
                //}
                var jo = JObject.Parse(str).ToObject<dynamic>();
                if (jo.errcode == 0)
                {
                    this.DbContext.Update(input);
                }
                else
                {
                    throw new Exception((string)jo.errmsg);
                }
            }
            else
            {
                throw new Exception("网络错误！");
            }
        }

        public void DeleteById(string id)
        {
            var input = this.DbContext.Query<CRM_KF_WXUser>().FilterDeleted().WhereIfNotNullOrEmpty(id, p => p.Id == id).FirstOrDefault();
            if (input == null)
            {
                throw new Exception($"id={id}的信息不存在！");
            }
            var str = WXAPI.DeleteKFAccount(this.DbContext, input.NickName, input.KFAccount, (new Random()).Next(100000, 1000000) + "");
            if (!String.IsNullOrEmpty(str))
            {
                //{
                //    "errcode" : 0,
                //    "errmsg" : "ok",
                //}
                var jo = JObject.Parse(str).ToObject<dynamic>();
                if (jo.errcode == 0)
                {
                    this.DbContext.SoftDelete<CRM_KF_WXUser>(id);
                }
                else
                {
                    throw new Exception((string)jo.errmsg);
                }
            }
            else
            {
                throw new Exception("网络错误！");
            }
        }

        public PagedData<CRM_KF_WXUser> GetList(Pagination pagination, string keyword)
        {
            var q = this.DbContext.Query<CRM_KF_WXUser>().FilterDeleted().WhereIfNotNullOrEmpty(keyword, p => p.NickName.Contains(keyword));
            q = q.OrderBy(o => o.NickName);
            PagedData<CRM_KF_WXUser> ret = q.TakePageData(pagination);
            return ret;
        }

        public void SetHeadUrl(string userId, string fileName, string fileUrl)
        {
            var input = this.DbContext.Query<CRM_KF_WXUser>().FilterDeleted().WhereIfNotNullOrEmpty(userId, p => p.Id == userId).FirstOrDefault();
            if (input == null)
            {
                throw new Exception("用户信息不存在！");
            }
            var str = WXAPI.SetKFAccountHeadUrl(this.DbContext, input.NickName, input.KFAccount, fileName);
            if (!String.IsNullOrEmpty(str))
            {
                //{
                //    "errcode" : 0,
                //    "errmsg" : "ok",
                //}
                var jo = JObject.Parse(str).ToObject<dynamic>();
                if (jo.errcode == 0)
                {//
                    input.HeadUrl = fileUrl;
                    this.DbContext.Update(input);
                }
                else
                {
                    throw new Exception((string)jo.errmsg);
                }
            }
            else
            {
                throw new Exception("网络错误！");
            }
        }
    }
}
